
var vm = new Vue({
    el: "#orderVue",
    data: {
        loading: true,
        searchs:'',         //搜索内容
        buy_car:[],         //购物车信息
        address:'',         //收货地址
        tel:'',             //收货电话
        send_date:'',       //送货日期
        type:'',            //类型
        ids:'',             //选择商品的id
        total_tax: 0,       //总增值税
        total_money: 0,     //收取总额
        delStr:'',
        user_id:'',
        user_name:'',
        web_token:'',
        oss_url:oss_url
    },
    methods: {
        productNum:function (index,product,list) {
            if (isNaN(product.num)){
                layer.msg('请输入数字</br>Please enter a number',{
                    skin:'winning-class',
                    time:3000
                });
                product.num = 1;
            }
            if (list.is_open!=1){
                layer.msg(list.supplier_name+',暂停营业!',{
                    skin:'winning-class',
                    time:3000
                });
                return false;
            }
            if (index == 2){  //增加
                  product.num = parseInt(product.num);
                  product.num += 1;
            }
            if (index == 1){ //减少
                product.num = parseInt(product.num);
                product.num -= 1;
            }
            if (product.num<1 || !product.num){
                product.num = 1;
            }
            if (product.num > product.stock){
                product.num = product.stock;
                layer.msg(product.product_name+',库存不足!',{
                    skin:'winning-class',
                    time:3000
                });
            }
            if (product.num <= product.stock){
                var total_price = parseFloat(product.price) * parseFloat(product.num);
                var total_tax   = parseFloat(product.tax)   * parseFloat(product.num);
                product.total_tax      = (total_tax).toFixed(2);
                product.total_price    = (parseFloat(total_tax) + parseFloat(total_price)).toFixed(2);
                if (product.is_discount == 1){
                    if ( parseFloat(product.num)>= product.condition_num){
                        var much = 0;
                        much = Math.floor(parseFloat(product.num)/parseFloat(product.condition_num));
                        var discountMoney   = (parseFloat(much) * parseFloat(product.discount_money)).toFixed(2);
                        product.total_price = (parseFloat(product.total_price) - parseFloat(discountMoney)).toFixed(2);
                        if (product.discount_type == 1){
                            product.discount = '￡'+discountMoney;
                        }else {
                            product.discount = product.give_product+'*'+much;
                        }
                    }else {
                        product.discount = '--';
                    }
                }
                if (product.total_price<0){
                    product.total_price = 0;
                }
                var totalTax    = 0;
                var totalMoney  = 0;
                for (var i = 0;i<list['product'].length;i++){
                    totalTax    += parseFloat(list['product'][i]['total_tax']);
                    totalMoney  += parseFloat(list['product'][i]['total_price']);
                }
                list['totalTax']    = (totalTax).toFixed(2);
                list['totalMoney']  = (totalMoney).toFixed(2);
            }
        },
        confirmOrder:function () {
            if (!this.user_id ){
               location.href = "/userweb/index/login.html";
            }
            validToken(); //验证tonken.
            var goods = this.buy_car;
            for (var i = 0;i<goods.length;i++){
                if (goods[i]['is_open']!=1){
                    layer.msg(goods[i]['supplier_name']+',暂停营业!',{
                        skin:'winning-class',
                        time:3000
                    });
                    return false;
                }
                // if (parseFloat(goods[i]['totalMoney'])<parseFloat(goods[i]['minimum'])){
                //     layer.msg(goods[i]['supplier_name']+',最低消费 ￡'+goods[i]['minimum'],{
                //         skin:'winning-class',
                //         time:3000
                //     });
                //     return false;
                // }
                goods[i]['remark']      = goods[i]['message'];
                if (goods[i]['remark'] == undefined){
                    goods[i]['remark'] = '';
                }
                for (var k = 0;k<goods[i]['product'].length;k++){
                    if (goods[i]['product'][k]['num'] > goods[i]['product'][k]['stock']){
                        layer.msg(goods[i]['product'][k]['product_name']+',库存不足!',{
                            skin:'winning-class',
                            time:3000
                        });
                        return false;
                    }
                    if(goods[i]['product'][k]['num']==0){
                        layer.msg(goods[i]['product'][k]['product_name']+',暂无库存!',{
                            skin:'winning-class',
                            time:3000
                        });
                        return false;
                    }
                    if (this.type == 1){
                        this.delStr += ','+goods[i]['product'][k]['cart_id'];
                    }
                    goods[i]['product'][k]['packing_proportion'] = goods[i]['product'][k]['specification']
                }
            }
            var url = '/api/Purchase/firmOrder';
            var data = {
                user_id:    this.user_id,
                type:       'WEB',
                goods:      goods
            };
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                // console.log(JSON.stringify(goods));
                if (res.body.status == 209){
                    layer.msg(res.body.msg,{
                        skin:'winning-class',
                        time:3000
                    });
                    return false;
                }
                if (res.body.status == 200) {
                    if (this.type == 1){
                        this.delProduct();
                    }
                    if (this.type == 2){
                        localStorage.removeItem('order_now')
                    }
                    location.href = "/userweb/index/myInfo.html";
                } else {
                    layer.msg('确认订单失败!',{
                        skin:'winning-class',
                        time:3000
                    });
                }
            }, function (res) {
            });
        },
        delProduct:function () {
            var url = '/userweb/Homepage/delCart';
            var data = {
                user_id:  this.user_id,
                delStr:   this.delStr
            };
            // console.log(data);return false;
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                /*var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
                this.loading = false;
            }, function (res) {
            });
        },
        req:function () {
            this.loading    = true;
            if (this.type ==1){
                var url  = '/userweb/Homepage/contrast';
                var data = {
                    user_id:  this.user_id,
                    ids:      this.ids
                };
            }
            if (this.type ==2){
                var url     = '/userweb/Homepage/orderNow';
                var data    = JSON.parse(localStorage.getItem('order_now'));
            }
            this.$http.post(url, data, {
                emulateJSON: true
            }).then(function (res) {
                if (res.data.status == 200) {
                    this.buy_car = res.data.buy_car;
                }
                if (this.buy_car.length==0){
                    $('#buyCar_list').html(this.nodata);
                }
                // console.log(this.buy_car);
                /*var _this = this;
                setTimeout(function () {
                    _this.loading = false;
                }, 300);*/
                this.loading = false;
                // console.log(this.buy_car);
            }, function (res) {
            });
        }
    },
    mounted: function () {
        var str             = location.href; //取得整个地址栏
        this.ids            = getUrlParam(str,'ids').split(",");
        this.type           = getUrlParam(str,'type').split(",");
        this.user_id        = localStorage.getItem('user_id');
        this.user_name      = localStorage.getItem('user_name');
        this.web_token      = localStorage.getItem('web_token');
        if (!this.user_id ){
            location.href = "/userweb/index/login.html";
        }
        validToken(); //验证tonken.
        this.req();
    }
});
